<?php
namespace app\index\controller;

use think\Request;
use think\Controller;
use app\index\model\Users as UsersModel;
use app\index\model\Devices as DevicesModel;
use app\index\model\CallRecords;

error_reporting(E_ALL ^ E_NOTICE);

class Bedside extends Controller {

    public function connectServer() {
        return succJson(['time'=>date('Y-m-d H:i:s')]);
    }

    public function isRegister($uuid) {
        if(!$uuid) return failJson('uuid不能为空');
        $device = new DevicesModel();
        $info = $device->where(['uuid'=>$uuid])->find();
        $is_reg = $info ? 1 : 0;
        return succJson(['isRegister'=>$is_reg]);
    }

    public function register(Request $request) {

        $uuid = qTrim($request->param('uuid'));
        $param = $request->param();

        $post_data = [
            'userName'=>qTrim($param['userName']),
            'phone'=>qTrim($param['phone']),
            'avatar'=>'https://img.alicdn.com/tfs/TB1J6_pWXT7gK0jSZFpXXaTkpXa-200-200.png'
        ];

        // $push_result = push::instance()->doPush(['uid'=>'uid1','content'=>'111111111111111111']);

        if(!$uuid)
        {
            return failJson('uuid不能为空');
        }

        $user_model = new UsersModel();

        $result = $user_model->field('id')
            ->where([
            'phone'=>qTrim($param['phone']),
        ])->find();

        if($result['id']) {
            return failJson('手机号已被注册');
        }


        $user_model->startTrans();
        $insert_user = $user_model->insertGetId($post_data);

        if($insert_user)
        {
            $insert_data = [
                'uuid'=>$uuid,
                'userId'=>$insert_user,
                'type'=>2
            ];
            $device = new DevicesModel();
            $device->startTrans();
            if( $device->insertGetId($insert_data) ) {
                $user_model->commit();
                $device->commit();
                return succJson(['userId'=>$insert_user,'uuid'=>$uuid]);
            }else{
                $user_model->rollback();
                return failJson('操作失败');
            }
        }
        else
        {
            return failJson('注册失败');
        }

    }

    public function call(Request $request) {
        $callFrom = qTrim($request->param('callFrom'));
        // $callTo = qTrim($request->param('callTo'));
        if(!$callFrom) {
            return failJson('拨打的uuid不能为空');
        }
//        if(!$callTo) {
//            return failJson('被呼叫的uuid不能为空');
//        }
        $device = new DevicesModel();
        $row = $device->where(['uuid'=>$callFrom])->find();
        if(!$row){
            return failJson('拨打的uuid不存在');
        }
        $row2 = $device->where(['type'=>1])->find();
        if(!$row2){
            return failJson('被呼叫的uuid不存在');
        }
        $callTo = $row2['uuid'];
        $result1 = $device->alias('d')
            ->field('d.uuid,d.type,d.userId, u.*')
            ->join('users u','d.userId = u.id')
            ->where(['uuid'=>$callFrom])
            ->find();

        // echo $device->getLastSql();
        if($result1){
            $device_type = $result1['type'];
            $push_result = push::instance()->doPush(['uid'=>$callTo,'msgBody'=>$result1,'msgType'=>'call']);
            $c_record = new CallRecords();
            if($push_result == 'ok'){
                $c_record->insertGetId([
                    'call_from'=>$callFrom,
                    'call_to'=>$callTo,
                    'status'=>1
                ]);
            }
            $list = $device->alias('d')
                ->field('d.uuid,d.type,d.userId, u.*')
                ->join('users u','d.userId = u.id')
                ->where(['type'=>$device_type])
                ->select();
            foreach ($list as $k => $val) {
                $res = push::instance()->doPush(['uid'=>$val['uuid'],'msgBody'=>$val,'msgType'=>'call']);
                if($res == 'ok') {
                    $c_record->insertGetId([
                        'call_from'=>$callFrom,
                        'call_to'=>$val['uuid'],
                        'status'=>1
                    ]);
                }
            }

            return succJson('操作成功');
        }else{
            return failJson('操作失败');
        }
    }

    public function callAnswer(Request $request) {
        $callId = qTrim($request->param('callId'));
        $c_record = new CallRecords();
        $row = $c_record->where(['id'=>$callId])->update(['status'=>2,'updateTime'=>date('Y-m-d H:i:s',time())]);
        if($row) {
            $push_result = push::instance()->doPush(['msgBody'=>'呼叫接通成功','msgType'=>'call']);
            return succJson($push_result);
        }else{
            return failJson('操作失败');
        }
    }

    public function getAnswerList(Request $request) {
        $callTo = qTrim($request->param('callTo'));
        if( empty($callTo) ){
            return failJson('callTo不能为空');
        }
        $call = new CallRecords();
        $list = $call->where(['call_to'=>$callTo])->select();
        return succJson($list);
    }

}
